package operators;

import java.util.Vector;

import operators.Attribute.type_map;

public class Relationship {
	
	//cardinality
	public static final int M2M = 1;
	public static final int M21 = 2;
	public static final int ID = 3;
	//public 
	public int car;
	
	public Vector<String> cardinality;
	public String name;
	public Vector<String> entityIndex;
	public Vector<Vector<String>> keys;
	public Vector<Attribute> attrlist;
	
	public Vector<String> vs ;
	
	public Relationship(String name, int car) {
		// TODO Auto-generated constructor stub
		this.name = name;
		this.keys = new Vector<Vector<String>>();
		this.attrlist = new Vector<Attribute>();
		this.car = car;
		//this.cardinality = new Vector<String>();
		this.entityIndex = new Vector<String>();
		this.vs = new Vector<String>();
	}
	
	public void addKeys(Vector<Vector<String>> keys) {
		
		int i;
	
		for(i = 0; i < keys.size(); i++)
			this.keys.add(keys.elementAt(i));
		
	}

	public void addAttr(Vector<String> attrlist) {
		
		if(car == M2M){
			for(int i = 0; i < attrlist.size(); i++){
				if(notInKeys(attrlist.elementAt(i))){
					Attribute a = new Attribute(attrlist.elementAt(i), type_map.many2one);
					this.attrlist.add(a);
				}
				else if(Integer.parseInt(vs.elementAt(i))<0){
					Attribute a = new Attribute(attrlist.elementAt(i), type_map.one2many);
					this.attrlist.add(a);
				}
			}
		} // if end M2M
		if(car == M21){
			for(int i = 0; i < attrlist.size(); i++){
				if(Integer.parseInt(vs.elementAt(i))==-1){
					Attribute a = new Attribute(attrlist.elementAt(i), type_map.many2one);
					this.attrlist.add(a);
				}
					
			}
		}
		
	}

	private boolean notInKeys(String s) {
		int i,j;
		for(i = 0; i < keys.size(); i++){
			for(j = 0;j < keys.elementAt(i).size();j++)
				if( s.equals(keys.elementAt(i).elementAt(j)) )
						return false;
		}
		return true;
	}

	public void addEntityIndex(Vector<String> vs) {
		
		for(int i = 0 ; i < vs.size(); i++){
			this.vs.add(vs.elementAt(i));
			if(Integer.parseInt(vs.elementAt(i)) >= 0)
				entityIndex.add(vs.elementAt(i));
		}
		for(int i = 0; i < entityIndex.size();i++){
			for(int j = i+1; j< entityIndex.size(); j++)
				if(entityIndex.elementAt(i).equals(entityIndex.elementAt(j)))
					entityIndex.remove(j);
		}
		
		this.cardinality = new Vector<String>(entityIndex.size());
		if(car == M2M)
			for(int i = 0 ; i < entityIndex.size(); i++)
				cardinality.add("m");
		else
		{
			//System.err.println("addEntityIndex(vs)@Relationship : cardinality != M2M");
		}
		
	}

	public void addAttr(Vector<String> vs, Vector<String> attrlist) {
	
		int i;
		for(i = 0; i < vs.size(); i++)
			if(vs.elementAt(i).equals("-1")){
				Attribute a = new Attribute(attrlist.elementAt(i), type_map.one2many);
				this.attrlist.add(a);
			}
		
	}

	public void addEntityIndex(Vector<String> vsless, Vector<String> vs) {
		
		for(int i = 0 ; i < vs.size(); i++){
			this.vs.add(vs.elementAt(i));
			if(Integer.parseInt(vs.elementAt(i)) >= 0)
				entityIndex.add(vs.elementAt(i));
		}
		for(int i = 0; i < entityIndex.size();i++){
			for(int j = i+1; j< entityIndex.size(); j++)
				if(entityIndex.elementAt(i).equals(entityIndex.elementAt(j)))
					entityIndex.remove(j);
		}
		
		this.cardinality = new Vector<String>(entityIndex.size());
		if(car == M21){
			for(int i = 0 ; i < entityIndex.size(); i++)
				cardinality.add("1");
			for(int i = 0; i < entityIndex.size(); i++){
				for(int j = 0 ; j < vsless.size(); j++){
					if(entityIndex.elementAt(i).equals(vsless.elementAt(j)))
						cardinality.set(i,"m");
				}
			}
		}
		else
		{
			//System.err.println("addEntityIndex(vsless,vs)@Relationship : cardinality != M21");
		}
			
	}

}
